POV-Ray : Newsgroups : povray.advanced-users : testing if a provided string matches strings in array : Re: testing if a provided string matches strings in array Server Time
1 Jul 2024 05:35:39 EDT (-0400)
  Re: testing if a provided string matches strings in array  
From: Chris B
Date: 30 Sep 2009 06:06:57
Message: <4ac32dc1@news.povray.org>
"Kene" <nomail@nomail> wrote in message 
news:web.4ac31453c0c633cf772dd76f0@news.povray.org...
>I have been trying to build a macro that will help place a povray object 
>using a
> relative coordinate system.
> ...
> My problem is that I cannot seem to wrap my mind around testing for the 
> cylinder
> id in the Cylinder_Id array. Can somebody help me out here?
>

Hi Kene,

The error you get in the message stream at the moment is "Attempt to access 
uninitialized array element". This is because your Save_Cylinder_Id macro 
adds to the size of the array after adding the last element, so there's 
always an empty array element at the end of the array. You can avoid testing 
the last element by using:

  #while ( Count < Cylinder_Id-Cylinder_Id_I)


It also adds "Object_Name" inside quotes to the array, which adds that 
string to the array, not the contents of the identifier Object_Name. This 
means your test for the actual object name is never successful. So you'd 
need:

  #declare Cylinder_Objects_Id[ Cylinder_Id_I ] = Object_Name;


But I have a more general concern that you're making life far more 
complicated for yourself than it needs to be. If you were working in a 
computer language with a full trace and debug toolset then indexing problems 
like this would be easy enough to trace through, but you don't have anything 
more powerful than the #debug statement to get you through this in POV-Ray. 
I'd recommend using more of the powerful features of POV-Ray SDL to vastly 
simplifying what you've got before going any further.

Anywhere you've got a macro for 1 or 2 lines of code I'd recommend 
eliminating it.

There's no need to keep so many different indices. You can store 3D vectors 
inside arrays and you can have multi-dimensional arrays, so you can store a 
start vector and an end vector in the same array:

#declare Cylinder_Locations = array [5][2];
#declare Cylinder_Locations[0][0] = <1,2,3>;
#declare Cylinder_Locations[0][1] = <4,5,6>;

This means you only need one index for all the cylinder arrays.

You can still access individual vector components e.g.
Cylinder_Locations[0,1].y
gives you the value '5'

So the whole Save_Cylinder_Loc macro comes down to 2 lines, at which point 
I'd scrap the macro altogether and simply inline the code.

It seems excessive to keep resizing the array (a relatively expensive 
option) each time you add an element. I don't think empty arrays take up 
much memory in POV-Ray, so you could declare your arrays with sufficient 
elements at the start and cut down on render times and on the amount of code 
you need to debug.

When using debug I'd recommend using it with the 'concat' function to help 
make messages stand out better in the message stream. Eg:

      #debug concat("Match: ",Cylinder_Objects_Id[ Count ],"\n")
      #debug concat("No Match: ",Cylinder_Objects_Id[ Count ],"\n")

I can offer more advice if you'd like, but it already sounds like I'm 
banging on a bit. :-)

Regards,
Chris B.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.